package test.combo;

import it.netsphere.nswing.annotation.NSEventMethod;
import it.netsphere.nswing.comp.NSComboBox;
import it.netsphere.nswing.comp.NSComboBoxRenderer;
import it.netsphere.nswing.comp.NSPanel;
import it.netsphere.nswing.event.NSEventMode;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.swing.JButton;
import javax.swing.JPanel;

public class ComboTestPanel extends NSPanel {

    private static org.slf4j.Logger LOG = org.slf4j.LoggerFactory.getLogger(ComboTestPanel.class);

    /**
	 * 
	 */
    private static final long serialVersionUID = -4343210106471190946L;

    public ComboTestPanel() {
        init();
    }

    private void init() {
        setLayout(new BorderLayout());
        JPanel p2 = new JPanel();
        p2.setLayout(new FlowLayout());
        p2.add(getComboName());
        p2.add(getComboDescr());
        p2.add(getDataButton());
        add(p2, BorderLayout.CENTER);

    }

    @Override
    protected Properties loadProperties(String name) {

        return null;
    }

    private NSComboBox comboName;

    private NSComboBox getComboName() {
        if (comboName == null) {
            comboName = createCombo("comboName", "name", ICodes.SELECTED_COMBO);
            comboName.setMapping(getSelectionDTO(), "selectedItem");
        }
        return comboName;
    }

    private NSComboBox comboDescr;

    @SuppressWarnings("unchecked")
    private NSComboBox getComboDescr() {
        if (comboDescr == null) {
            comboDescr = new NSComboBox();
            comboDescr.setRenderer(new NSComboBoxRenderer("descr"));
        }
        return comboDescr;
    }

    private JButton dataButton;

    private JButton getDataButton() {
        if (dataButton == null) {
            dataButton = new JButton();
            dataButton.setText("Add Items");
            dataButton.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent e) {
                    List<ComboDTO> l = new ArrayList<ComboDTO>();
                    for (int c = 0; c < 5; c++) {
                        ComboDTO dto = new ComboDTO();
                        dto.setName("name-" + c);
                        dto.setDescr("descr-dto");
                        l.add(dto);
                    }
                    ;

                    final ComboDTO selectionItem = ComboDTO.create("---- select ----", "---- select ----");
                    // final ComboDTO selectionItem = null;

                    getComboName().setData(l, selectionItem);
                    getComboDescr().setData(l, selectionItem);
                }
            });
        }

        return dataButton;
    }

    private SelectionDTO selectionDTO;

    private SelectionDTO getSelectionDTO() {
        if (selectionDTO == null) {
            selectionDTO = new SelectionDTO();
        }

        return selectionDTO;
    }

    @NSEventMethod(//
    code = ICodes.SELECTED_COMBO,//
    mode = NSEventMode.REQUEST)
    public void f1() {
        LOG.debug("aaaaaaaaaaa");
    }

}
